package com.youhome.housing.controller;

import com.aliyun.oss.OSSClient;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.housing.dto.DTOVideoUpload;
import com.youhome.service.service.HouseVideoService;
import com.youhome.service.service.OSSService;
import com.youhome.service.service.VideoTransCodingService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URL;
import java.util.Date;

/**
 * Created by chen365 on 2019/03/22.
 */
@RestController
@Api(tags = "视频", position = 4)
@RequestMapping(value = "/video")
public class VideoController {

    @Autowired
    private OSSService ossService;
    @Autowired
    private VideoTransCodingService videoTransCodingService;
    @Autowired
    private HouseVideoService houseVideoService;

    @ApiOperation(value = "获取oss临时token", notes = "获取oss临时token", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/token")
    public Object token() throws CustomException {
        return new Result(ResultCode.SUCCESS, ossService.getVideoToken());
    }

    @ApiOperation(value = "上传房源视频", notes = "上传房源视频", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/video")
    public Object uploadVideo(@ApiParam @Validated DTOVideoUpload dtoVideoUpload) throws CustomException {
        videoTransCodingService.transCoding(dtoVideoUpload.getHouseId(), dtoVideoUpload.getName(), dtoVideoUpload.getSize(), dtoVideoUpload.getUrl());
        return new Result<>(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "获取房源视频", notes = "获取房源视频", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "query"),
    })
    @GetMapping(value = "/video")
    public Object videoList(String houseId) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, houseVideoService.selectVideoByHouseId(houseId));
    }

    @ApiOperation(value = "删除房源视频", notes = "删除房源视频", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "videoId", value = "视频ID", required = true, paramType = "query"),
    })
    @DeleteMapping(value = "/video")
    public Object deleteVideo(String videoId) throws CustomException {
        houseVideoService.deleteByEnabled(videoId);
        return new Result<>(ResultCode.SUCCESS);
    }

    public static void main(String[] args) {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-zhangjiakou.aliyuncs.com";
// 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = "LTAIIbnagn6Mvi3m";
        String accessKeySecret = "RwpTWpnWUJzOqY2mgWNxHLkKLMN3iJ";
        String bucketName = "youhome";
        String objectName = "test/60be27c6-fab1-4633-b3a2-d6a582a0e251.jpg";
        //http://youhome.oss-cn-zhangjiakou.aliyuncs.com/test/60be27c6-fab1-4633-b3a2-d6a582a0e251.jpg
// 创建OSSClient实例。
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

// 设置URL过期时间为1小时。
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
// 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
        URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
        System.err.println(url.toString());
        System.err.println(url.getPath());
// 关闭OSSClient。
        ossClient.shutdown();
    }
}
